import { ref, onUnmounted } from 'vue';
import type { Ref } from 'vue';

export default function useWindowDrag(): {
	isKeyDown: Ref<boolean>;
	handleMouseDown: (event: MouseEvent) => void;
} {
	const isKeyDown = ref(false);
	const initialX = ref(0);
	const initialY = ref(0);

	const handleMouseDown = (event: MouseEvent): void => {
		isKeyDown.value = true;
		initialX.value = event.x;
		initialY.value = event.y;

		const onMouseMove = (ev: MouseEvent): void => {
			if (isKeyDown.value) {
				const data = {
					x: ev.screenX - initialX.value,
					y: ev.screenY - initialY.value
				}

				window.electron.ipcRenderer.invoke('renderer-to-main', { name: 'custom-adsorption', data });
			}
		};

		document.addEventListener('mousemove', onMouseMove);

		const onMouseUp = (): void => {
			isKeyDown.value = false;
			document.removeEventListener('mousemove', onMouseMove as EventListener);
		};

		document.addEventListener('mouseup', onMouseUp);

		// 清理事件监听器
		onUnmounted(() => {
			document.removeEventListener('mousemove', onMouseMove as EventListener);
			document.removeEventListener('mouseup', onMouseUp as EventListener);
		});
	};

	return {
		isKeyDown,
		handleMouseDown,
	};
}